package com.yuyou.fn.platform.service.impl;

import com.yuyou.fn.platform.service.ISysRoleResourceService;
import com.yuyou.fn.platform.service.ISysUserRoleService;
import com.yuyou.fn.common.util.TimeUtils;
import org.springframework.stereotype.Service;

import com.yuyou.fn.platform.mapper.SysRoleMapper;
import com.yuyou.fn.platform.entity.SysRole;
import com.yuyou.fn.platform.service.ISysRoleService;
import com.github.wens.framework.service.impl.SuperServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * SysRole 表数据服务层接口实现类
 *
 */
@Service
public class SysRoleServiceImpl extends SuperServiceImpl<SysRoleMapper, SysRole,Long> implements ISysRoleService {

    @Resource
    private ISysRoleResourceService sysRoleResourceService ;

    @Resource
    private ISysUserRoleService sysUserRoleService ;

    @Override
    public void saveOrUpdateRole(Long roleId , String roleName){
        if(roleId == null ){
            SysRole sysRole = new SysRole();
            sysRole.setRoleName(roleName);
            sysRole.setCreateTime(TimeUtils.now());
            sysRole.setUpdateTime(TimeUtils.now());
            insertSelective(sysRole);
        }else{
            SysRole sysRole = new SysRole();
            sysRole.setRoleName(roleName);
            sysRole.setRoleId(roleId);
            sysRole.setUpdateTime(TimeUtils.now());
            updateSelectiveById(sysRole);
        }
    }

    @Transactional
    @Override
    public void deleteRole(Long roleId) {
        sysUserRoleService.deleteUserRole( null , roleId );
        sysRoleResourceService.deleteRoleResource(roleId);
        deleteById(roleId);
    }

    @Override
    public List<SysRole> findAll() {
        return findListByExample(SysRole.createExample());
    }
}